package com.example.demo.leetcode.classics150;

import java.util.HashSet;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _46存在重复元素II
 * @description
 * @date 2023/07/23 9:20
 * <p>
 * ******************************************************
 */
public class _46存在重复元素II {

    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashSet<Integer> set = new HashSet<>();

        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i])) {
                return true;
            }
            set.add(nums[i]);
            if (set.size() > k) {
                set.remove(nums[i - k]);
            }
        }
        return false;
    }


    public static void main(String[] args) {
        _46存在重复元素II s = new _46存在重复元素II();
        System.out.println(s.containsNearbyDuplicate(new int[]{1, 2, 2, 3}, 3));
    }

}
